package cn.qyd.kaoshi;
import java.util.*;

/**
 * @author 邱运铎
 * @date 2024-04-13 2:00
 */
class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int n = 3;
        int[][] edges = new int[][]{{0, 1},{0, 2}};
        int champion = solution.findChampion(n, edges);
        System.out.println(champion);
    }
    public int findChampion(int n, int[][] edges) {
        List<Integer>[] listArr = new List[n];
        Arrays.setAll(listArr, i -> new ArrayList<>());

        for (int[] edge : edges) {
            listArr[edge[0]].add(edge[1]);
        }

        List<Integer>[] result = new List[n];
        Arrays.setAll(result, i -> new ArrayList<>());
        int[] visited = new int[n];
        Arrays.fill(visited, -1);

        for (int i = 0; i < n; i++) {
            dfs(listArr, result, i, i, visited);
        }
        System.out.println(Arrays.toString(listArr));
        System.out.println(Arrays.toString(result));
        int resultIndex = -1;
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (result[i].size() == n - 1) {
                count++;
                resultIndex = i;
            }
        }

        return count > 1 ? -1 : resultIndex;

    }

    public void dfs(List<Integer>[] listArr, List<Integer>[] result, int index, int origin, int[] visited) {
        List<Integer> list = listArr[index];
        System.out.println(list);
        for (Integer node : list) {
            System.out.println(node);
            System.out.println(Arrays.toString(visited));
            if (visited[node] != origin) {
                visited[node] = origin;
                result[origin].add(node);
                System.out.println(node);
                dfs(listArr, result, node, origin, visited);
            }
        }
    }
}
